home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 8275 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.2 KB  |  80 lines

  1. Path: kom22.ethz.ch!conrad
  2. From: conrad@kom22.ethz.ch (Christian Conrad)
  3. Newsgroups: comp.lang.c++
  4. Subject: Programmed-in relationships (Stroustrup Example)
  5. Date: 16 Feb 1996 11:34:16 GMT
  6. Organization: Swiss Federal Institute of Technology (ETHZ)
  7. Message-ID: <4g1q3o$scu@elna.ethz.ch>
  8. NNTP-Posting-Host: kom22-e.ethz.ch
  9. X-Newsreader: TIN [version 1.2 PL1]
  10.  
  11. Hello,
  12.  
  13. I am having some trouble with programmed-in relationships, especially
  14. with the delegation notion.
  15.  
  16. In the following piece of code, only the 2 constructor functions must
  17. be visible for the application programmer (all the rest is implementation
  18. detail and must not be accessible...). I would like to access B::f() by
  19. delegation through A::p in the constructor of class A.
  20. --->Compiler output: B::f() is not accessible from A::A(B*).
  21.  
  22. I see only one solution:
  23.     - declare B::f() as a friend in both A and B classes,
  24.       BUT what prevents the "user" to create a C class with a
  25.           friend f() declaration, and then to "abuse" of my
  26.           function f() ?
  27.  
  28.  
  29. A similar example is provided in the sec. ed. of the Stroustrup's C++ ...
  30. (page 421).
  31.  
  32.     class B {
  33.         void f() {}     // current implementation of the function
  34.       public:
  35.         B();
  36.     };
  37.  
  38.     class A {
  39.         B *p;           // delegation through p
  40.       public:
  41.         A(B *pb);
  42.     };
  43.  
  44.     A::A(B *pb) {
  45.         p=pb;
  46.         p->f();         // Not possible to access the B object...
  47.     };
  48.  
  49.     main()
  50.     {
  51.         B b;
  52.         A a(&b);
  53.     };
  54.  
  55. Is my "reluctance" to declare f() as a friend justified or not in this case ?
  56. As I am a beginner in C++ (and also OOP), I would appreciate any advice
  57. to improve this design or to correct it if it is totally wrong !
  58.  
  59. Thanks,
  60. Christian
  61.  
  62. --
  63.  
  64.  ---------------------------     % % % % %     ---------------------------
  65.                                         %
  66.  Christian CONRAD                      %            
  67.                                       %        Tel. (+41) 1 / 632 7015       
  68.  ETH-Zurich                          %         Fax  (+41) 1 / 632 1035        
  69.    Technische Informatik und        %          E-Mail: conrad@tik.ee.ethz.ch   
  70.    Kommunikationsnetze (TIK)       %       
  71.                                   %        
  72. ----------------------------     % % % % %     ---------------------------
  73.  
  74.  
  75.  
  76.  
  77.  
  78.  
  79.  
  80.